package com.hkts.naturalgas.performance.controller;


import com.hkts.naturalgas.performance.entity.HkAssessmentIndex;
import com.hkts.naturalgas.performance.entity.HkFormEntity;
import com.hkts.naturalgas.performance.performanceVo.EntityByFlowInstanceIdDataVo;
import com.hkts.naturalgas.performance.performanceVo.FormAndItemResultVo;
import com.hkts.naturalgas.performance.performanceVo.FormAndItemsVo;
import com.hkts.naturalgas.performance.performanceVo.IndexAndFormIdVo;
import com.hkts.naturalgas.performance.service.IHkFormEntityService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.hkts.naturalgas.utils.CustomPageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.hkts.naturalgas.performance.entity.HkAssessmentIndex.SCORE_TYPE_DELETE;

/**
 * @author yck
 * @since 2020-12-22
 */
@RestController
@RequestMapping("/performance/hkFormEntity")
public class HkFormEntityController {
    private final Logger logger = LoggerFactory.getLogger(HkFormEntityController.class);

    @Autowired
    public IHkFormEntityService iHkFormEntityService;

    /**
     * 分页查询数据
     *
     * @param page         分页信息
     * @param hkFormEntity 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHkFormEntityPageList")
    public Result<Object> getHkFormEntityPageList(Page<HkFormEntity> page, HkFormEntity hkFormEntity) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHkFormEntityService.selectPage(page, hkFormEntity)));
    }

    /**
     * 保存和修改公用的
     *
     * @param hkFormEntity 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkFormEntitySave")
    public Result<Object> hkFormEntitySave(HkFormEntity hkFormEntity) {
        iHkFormEntityService.saveOrUpdate(hkFormEntity);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hkFormEntityGetById")
    public Result<Object> hkFormEntityGetById(String id) {
        HkFormEntity byId = iHkFormEntityService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkFormEntityDelete")
    public Result<Object> hontUserDelete(String id) {
        iHkFormEntityService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkFormEntityBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHkFormEntityService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

    /*
     * 根据用户id 获取表单
     *
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getFormEntityByUserId")
    public Result<Object> getFormEntityByUserId(String userId) throws Exception {
        List<FormAndItemResultVo> vo = iHkFormEntityService.getFormEntityByUserId(userId);

        return ResultUtil.data(vo);
    }

    /*
     * 根据岗位id 获取表单
     *
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getFormEntityByFlowIdAndPostId")
    public Result<Object> getFormEntityByFlowIdAndPostId(String postId,String flowId,String templateVersion){
        EntityByFlowInstanceIdDataVo entityByFlowInstanceIdDataVo = iHkFormEntityService.selectFormEntityByflowIdAndPostId(postId, flowId,templateVersion);
        return ResultUtil.data(entityByFlowInstanceIdDataVo);
    }

    /*
     * 根据流程id获取待审批表单
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getFormEntityByProessId")
    public Result<Object> getFormEntityByProessId(String proessId){
        List<FormAndItemResultVo> vo = iHkFormEntityService.getFormEntityByProessId(proessId);

        return ResultUtil.data(vo);
    }

    /*
     * 根据流程id获取待审批表单 V1.5
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getFormEntityByFlowInstanceId")
    public Result<Object> getFormEntityByFlowInstanceId(String flowInstanceId){
        EntityByFlowInstanceIdDataVo vo = iHkFormEntityService.getFormEntityByFlowInstanceId(flowInstanceId);

        return ResultUtil.data(vo);
    }

}
